package at.ac.thornwishstalon.ocrapp.main.fragments;

import android.app.ActionBar.Tab;
import android.app.ActionBar.TabListener;
import android.app.Activity;
import android.app.Fragment;
import android.app.FragmentTransaction;


public class OcrappTabListener<T extends Fragment> implements TabListener {
	private final String TAG = "OcrappTabListener";
	private final Class<T> clazz;
	private Activity main;
	private Fragment fragment;
	private Fragment otherFragment; 
	
	
	
	public OcrappTabListener(Activity activity, Class<T> clazz){
		main= activity;
		this.clazz=clazz;
		
		
	}
	

	public void onTabReselected(Tab tab, FragmentTransaction ft) {
		
		//nothing to do
	}

	public void onTabSelected(Tab tab, FragmentTransaction ft) {
		if(fragment==null){
			fragment= Fragment.instantiate(main, clazz.getName());
			ft.add(android.R.id.content, (Fragment) fragment, TAG);
		}else{
			ft.attach((Fragment) fragment);
		}
	}

	public void onTabUnselected(Tab tab, FragmentTransaction ft) {
		if(fragment!=null){
			ft.detach((Fragment) fragment);
		}
			
			//ft.detach(fragment);
	}
	
	public Fragment getFragment(){
		return fragment;
	}
	
	public void setOtherFragment(Fragment other){
		this.otherFragment=other;
		}
	

}
